//
//  MLWTipsICookDetailMainCell.m
//  KITCHEN_STORIES
//
//  Created by 孟令通 on 16/6/12.
//  Copyright © 2016年 MLW. All rights reserved.
//

#import "MLWTipsICookDetailMainCell.h"

#import "MLWBaseView.h"

@implementation MLWTipsICookDetailMainCell


- (void)dealloc
{
    [_mainImageView release];
    
    [_dashesName release];
    
    [_dishes_title release];
    
    [_tagImageView release];
    
    [_hardLevelImage release];
    
    [_hardLevelText release];
    
    [_coockTimeImage release];
    
    [_coockTimeText release];
    
    [_tasteImage release];
    
    [_tasteText release];
    
    [super dealloc];
}

- (instancetype)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {
        
        // 主图片
        self.mainImageView = [[UIImageView alloc] init];
        
        [self.contentView addSubview:_mainImageView];
        [_mainImageView release];
        
        // 遮罩层
        self.maskViewTop = [[UIView alloc] init];
        
        [self.contentView addSubview:_maskViewTop];
        [_maskViewTop release];
        
        self.maskViewBottom = [[UIView alloc] init];
        
        [self.contentView addSubview:_maskViewBottom];
        [_maskViewBottom release];
        
        // 食材准备按钮
        self.foodPrepareBtn = [UIButton buttonWithType:UIButtonTypeCustom];
        [self.foodPrepareBtn setTitle:@"食材准备" forState:UIControlStateNormal];
        [self.foodPrepareBtn.titleLabel setFont:[UIFont systemFontOfSize:14]];
        
        [self.contentView addSubview:_foodPrepareBtn];
        [_foodPrepareBtn release];
        
        // 步骤按钮
        self.stepBtn = [UIButton buttonWithType:UIButtonTypeCustom];
        [self.stepBtn setTitle:@"制作步骤" forState:UIControlStateNormal];
        [self.stepBtn.titleLabel setFont:[UIFont systemFontOfSize:14]];
        
        [self.contentView addSubview:_stepBtn];
        [_stepBtn release];
        
        // 播放按钮
        self.playBtn = [UIButton buttonWithType:UIButtonTypeCustom];
        [self.playBtn setImage:[UIImage imageNamed:@"tipsVideoDetail"] forState:UIControlStateNormal];
        [self.playBtn addTarget:self action:@selector(didClickedMainPlayButton:) forControlEvents:UIControlEventTouchUpInside];
        
        [self.contentView addSubview:_playBtn];
        [_playBtn release];
        
        // 名称
        self.dashesName = [[UILabel alloc] init];
        self.dashesName.textColor = [UIColor colorWithRed:0.33 green:0.33 blue:0.33 alpha:1.00];
        
        [self.contentView addSubview:_dashesName];
        [_dashesName release];
        
        // 描述
        self.dishes_title = [[UILabel alloc] init];
        [self.dishes_title setFont:[UIFont systemFontOfSize:14]];
        self.dishes_title.numberOfLines = 0;
        
        [self.contentView addSubview:_dishes_title];
        [_dishes_title release];
        
        // 标签
        self.tagImageView = [[UIImageView alloc] init];
        self.tagImageView.image = [UIImage imageNamed:@"tipsTag"];
        
        [self.contentView addSubview:_tagImageView];
        [_tagImageView release];
        
        self.hardLevelImage = [[UIImageView alloc] init];
        self.hardLevelImage.image = [UIImage imageNamed:@"tipsLevel"];
        
        [self.contentView addSubview:_hardLevelImage];
        [_hardLevelImage release];
        
        self.hardLevelText = [[UILabel alloc] init];
        self.hardLevelText.textAlignment = NSTextAlignmentCenter;
        [self.hardLevelText setFont:[UIFont systemFontOfSize:14]];
        self.hardLevelText.textColor = [UIColor colorWithRed:0.33 green:0.33 blue:0.33 alpha:1.00];
        
        [self.contentView addSubview:_hardLevelText];
        [_hardLevelText release];
        
        self.coockTimeImage = [[UIImageView alloc] init];
        self.coockTimeImage.image = [UIImage imageNamed:@"tipsTime"];
        
        [self.contentView addSubview:_coockTimeImage];
        [_coockTimeImage release];
        
        self.coockTimeText = [[UILabel alloc] init];
        self.coockTimeText.textAlignment = NSTextAlignmentCenter;
        [self.coockTimeText setFont:[UIFont systemFontOfSize:14]];
        self.coockTimeText.textColor = [UIColor colorWithRed:0.33 green:0.33 blue:0.33 alpha:1.00];
        
        [self.contentView addSubview:_coockTimeText];
        [_coockTimeText release];
        
        
        self.tasteImage = [[UIImageView alloc] init];
        self.tasteImage.image = [UIImage imageNamed:@"tipsTaste"];
        
        [self.contentView addSubview:_tasteImage];
        [_tasteImage release];
        
        
        self.tasteText = [[UILabel alloc] init];
        self.tasteText.textAlignment = NSTextAlignmentCenter;
        [self.tasteText setFont:[UIFont systemFontOfSize:14]];
        self.tasteText.textColor = [UIColor colorWithRed:0.33 green:0.33 blue:0.33 alpha:1.00];
        
        [self.contentView addSubview:_tasteText];
        [_tasteText release];
        
        self.goBackBtn = [UIButton buttonWithType:UIButtonTypeCustom];
        [self.goBackBtn setImage:[UIImage imageNamed:@"arrowLeft"] forState:UIControlStateNormal];
        [self.goBackBtn addTarget:self action:@selector(didClickedGoBackBtn:) forControlEvents:UIControlEventTouchUpInside];
        
        [self.contentView addSubview:_goBackBtn];
        [_goBackBtn release];
        
        
    }
    return self;
}

-(void)layoutSubviews
{
    [super layoutSubviews];
    
    [self.mainImageView makeConstraints:^(MASConstraintMaker *make) {
        
        make.edges.equalTo(self.contentView).with.insets(UIEdgeInsetsMake(0, 0, 182, 0));
        
    }];
    
    self.mainImageView.contentMode = UIViewContentModeScaleAspectFill;
    self.mainImageView.clipsToBounds = YES;
    
    [self.maskViewTop makeConstraints:^(MASConstraintMaker *make) {
        
        make.top.equalTo(self.mainImageView.top).offset(0);
        
        make.left.equalTo(self.contentView).offset(0);
        
        make.right.equalTo(self.contentView).offset(0);
        
        make.height.equalTo(100);
        
    }];
    
    CAGradientLayer *maskLayerTop = [CAGradientLayer layer];
    
    maskLayerTop.frame = self.maskViewTop.bounds;
    
    [self.maskViewTop.layer addSublayer:maskLayerTop];
    
    maskLayerTop.colors = @[(__bridge id)[UIColor colorWithRed:0.12 green:0.13 blue:0.13 alpha:0.30].CGColor,
                         (__bridge id)[UIColor colorWithRed:0.04 green:0.04 blue:0.04 alpha:0.00].CGColor];
    
    //颜色分割线
    maskLayerTop.locations = @[@(0), @(1)];
    
    //起始点
    maskLayerTop.startPoint = CGPointMake(0, 0);
    
    //结束点
    maskLayerTop.endPoint = CGPointMake(0, 1);
    
    [self.maskViewBottom makeConstraints:^(MASConstraintMaker *make) {
        
        make.bottom.equalTo(self.mainImageView.bottom).offset(0);
        
        make.left.equalTo(self.contentView).offset(0);
        
        make.right.equalTo(self.contentView).offset(0);
        
        make.height.equalTo(50);
        
    }];
    
    CAGradientLayer *maskLayerBottom = [CAGradientLayer layer];
    
    maskLayerBottom.frame = self.maskViewBottom.bounds;
    
    [self.maskViewBottom.layer addSublayer:maskLayerBottom];
    
    maskLayerBottom.colors = @[(__bridge id)[UIColor colorWithRed:0.02 green:0.02 blue:0.02 alpha:0.00].CGColor,
                            (__bridge id)[UIColor colorWithRed:0.12 green:0.13 blue:0.13 alpha:0.30].CGColor];
    
    //颜色分割线
    maskLayerBottom.locations = @[@(0), @(1)];
    
    //起始点
    maskLayerBottom.startPoint = CGPointMake(0, 0);
    
    //结束点
    maskLayerBottom.endPoint = CGPointMake(0, 1);
    
    [self.foodPrepareBtn makeConstraints:^(MASConstraintMaker *make) {
        
        make.bottom.equalTo(self.mainImageView).offset(0);
        
        make.left.equalTo(self.contentView).offset(0);
        
        make.width.equalTo(ScreenWidth / 2.0);
        
        make.height.equalTo(30);
        
    }];

    
    [self.stepBtn makeConstraints:^(MASConstraintMaker *make) {
        
        make.bottom.equalTo(self.mainImageView).offset(0);
        
        make.right.equalTo(self.contentView).offset(0);
        
        make.width.equalTo(ScreenWidth / 2.0);
        
        make.height.equalTo(30);
        
    }];

    
    [self.playBtn makeConstraints:^(MASConstraintMaker *make) {
        
        make.left.equalTo(self.contentView).offset((ScreenWidth - 50) / 2.0);
        
        make.width.equalTo(50);
        
        make.height.equalTo(50);
        
        make.top.equalTo(self.contentView).offset(ScreenWidth / 2.0);
        
    }];
    
    [self.dashesName makeConstraints:^(MASConstraintMaker *make) {
        
        make.top.equalTo(self.mainImageView.bottom).offset(10);
        
        make.left.equalTo(self.contentView).offset(10);
        
        make.height.equalTo(20);
        
        make.width.equalTo(200);
        
    }];
    
    [self.tagImageView makeConstraints:^(MASConstraintMaker *make) {
        
        make.top.equalTo(self.dashesName.bottom).offset(15);
        
        make.left.equalTo(self.contentView).offset(10);
        
        make.width.equalTo(18.5);
        
        make.height.equalTo(18.5);
        
    }];
    
    
    [self.dishes_title makeConstraints:^(MASConstraintMaker *make) {
        
        make.left.equalTo(self.contentView).offset(10);
        
        make.top.equalTo(self.tagImageView).offset(20);
        
        make.width.equalTo(ScreenWidth - 30);
        
        make.height.equalTo(40);
        
    }];
    
    [self.hardLevelImage makeConstraints:^(MASConstraintMaker *make) {
        
        make.width.equalTo(24);
        
        make.height.equalTo(24);
        
        make.left.equalTo(self.contentView).offset((100 / 750.0) * ScreenWidth);
        
        make.top.equalTo(self.dishes_title.bottom).offset(15);
        
    }];
    
    [self.coockTimeImage makeConstraints:^(MASConstraintMaker *make) {
        
        make.width.equalTo(24);
        
        make.height.equalTo(24);
        
        make.left.equalTo(self.hardLevelImage.right).offset((212 / 750.0) * ScreenWidth);
        
        make.top.equalTo(self.dishes_title.bottom).offset(15);
        
    }];
    
    [self.tasteImage makeConstraints:^(MASConstraintMaker *make) {
        
        make.width.equalTo(24);
        
        make.height.equalTo(24);
        
        make.left.equalTo(self.coockTimeImage.right).offset((200 / 750.0) * ScreenWidth);
        
        make.top.equalTo(self.dishes_title.bottom).offset(15);
        
    }];
    
    [self.hardLevelText makeConstraints:^(MASConstraintMaker *make) {
        
        make.left.equalTo(self.contentView).offset(0);
        
        make.top.equalTo(self.hardLevelImage.bottom).offset(0);
        
        make.width.equalTo(ScreenWidth / 3.0);
        
        make.height.equalTo(40);
        
    }];
    
    [self.coockTimeText makeConstraints:^(MASConstraintMaker *make) {
        
        make.left.equalTo(self.hardLevelText.right).offset(0);
        
        make.top.equalTo(self.coockTimeImage.bottom).offset(0);
        
        make.width.equalTo(ScreenWidth / 3.0);
        
        make.height.equalTo(40);
        
    }];
    
    [self.tasteText makeConstraints:^(MASConstraintMaker *make) {
        
        make.right.equalTo(self.contentView).offset(0);
        
        make.top.equalTo(self.tasteImage.bottom).offset(0);
        
        make.width.equalTo(ScreenWidth / 3.0);
        
        make.height.equalTo(40);
        
    }];
    
    [self.goBackBtn makeConstraints:^(MASConstraintMaker *make) {
        
        make.top.equalTo(self.contentView).offset(40);
        
        make.left.equalTo(self.contentView).offset(20);
        
        make.width.equalTo(32);
        
        make.height.equalTo(32);
        
    }];
    
}

-(void)didClickedGoBackBtn:(UIButton *)sender
{
    [self.MLWDelegate goBackToFrontPage];
}

-(void)setModel:(MLWTipsICookVideoDetailModel *)model
{
    if (_model != model) {
        
        [_model release];
        
        _model = [model retain];
        
        [self.mainImageView sd_setImageWithURL:[NSURL URLWithString:model.image]];
        
        self.dashesName.text = _model.dashes_name;
        
        self.dishes_title.text = _model.material_desc;
        
        self.hardLevelText.text = [NSString stringWithFormat:@"难度 : %@", _model.hard_level];
        
        self.coockTimeText.text = [NSString stringWithFormat:@"烹饪时间 : %@", _model.cooke_time];
        
        self.tasteText.text = [NSString stringWithFormat:@"口味 : %@", _model.taste];
    }
}

#pragma mark 播放按钮点击方法
- (void) didClickedMainPlayButton:(UIButton *)sender
{
    
    [self.MLWDelegate pushToVideoPlayerControllerWithUrl:_model.material_video];
}


@end
